<?php
@session_start();

class nfwk_model extends nfwk {

    public $chars = "abc2def3ghj4klm5npq6rst7uvw8xyz9";
    public $offset = 1000;

    
    public function encode_uid($inputstring) {
        // can't handle numbers larger than 2^31-1 = 2147483647
        $returnstring = "";
        $base = strlen($this->chars);

        do {
            $i = $inputstring % $base;
            $returnstring = $this->chars[$i] . $returnstring;
            $inputstring = ($inputstring - $i) / $base;
        } while($inputstring > 0);

        return $returnstring;
    }
    //--------------------------------------------------------------------------
    

    public function decode_uid($inputstring) {

        $returnstring = "";
        $base = strlen($this->chars);
        
        $len = strlen($inputstring);
        $returnstring = 0;
        $arr = array_flip(str_split($this->chars));
        for($i = 0; $i < $len; ++$i) {
            $returnstring += $arr[$inputstring[$i]] * pow($base, $len-$i-1);
        }
        return $returnstring;
    }
    //--------------------------------------------------------------------------
    
    
    public function insert_into_db($modelname) {
        
        $modelname = trim(strtolower($modelname));
        
        nfwk::open_db();
        $this->_DBCONN->Execute("INSERT INTO nfwk_models (name) VALUE ('".$modelname."')");
        
        $modeldata = $this->_DBCONN->GetRow("SELECT id, name FROM nfwk_models where name = '".$modelname."'");
        
        $modelid = $modeldata['id'];
        $offmodelid = $this->offset + $modelid;
        $muid = nfwk_model::encode_uid($offmodelid);
        
        echo $ModelID." ".$OffsetModelID." ".$ModelUID;
        $this->_DBCONN->Execute("UPDATE nfwk_models SET muid = '".$muid."' WHERE id = ".$modelid." AND name = '".$modelname."'");
        
        $returnstring = "<strong>Operazione eseguita con successo!</strong><br />".
        $returnstring .= "Il modello <strong>".$modelname."</strong> &egrave; stato correttamente creato.<br />";
        
        return $returnstring;
        
    }
    //--------------------------------------------------------------------------
    
    
    public function verify_existance($modelname) {
        
        echo "PPP";
        
    }
    //--------------------------------------------------------------------------
}
?>
